﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_KQKT
{
    public partial class FormBaoCaoMau8 : MyFromBase
    {
        private PrepareDataForTreeView DataForTreeView = new PrepareDataForTreeView();
        private static int IDDonVi;
        private static int KyBaoCao;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermission();
            lblNotice.Visible = false;
            if (!IsPostBack)
            {
                BindDataToDropDownList(); ;
                DataForTreeView.PopulateRootLevel(trvDonVi);
            }
            KyBaoCao = Convert.ToInt32(ddlKyBaoCao.SelectedValue);
        }

        protected void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("BaoCaoMau8");
            if (Role == null || Role["Xem"] == 0)
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }
        protected void trvDonVi_SelectedNodeChanged(object sender, EventArgs e)
        {
            lblDonVi.Text = "Đơn vị đã chọn : " + trvDonVi.SelectedNode.Text;
            IDDonVi = Convert.ToInt32(trvDonVi.SelectedValue);
        }

        protected void trvDonVi_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            DataForTreeView.PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
        }

        protected void BindDataToDropDownList()
        {
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM='KyBaoCao'");
            ddlKyBaoCao.DataSource = dt;
            ddlKyBaoCao.DataTextField = "TenDM";
            ddlKyBaoCao.DataValueField = "ID";
            ddlKyBaoCao.DataBind();
        }

        protected void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            if (trvDonVi.SelectedNode == null)
            {
                lblNotice.Visible = true;
                lblNotice.InnerText = "Vui lòng lựa chọn đơn vị muốn xem báo cáo";
            }
            else
            {
                List<SqlParameter> lstParams = new List<SqlParameter>();

                if (chkTongHop.Checked)
                {
                    lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                    lstParams.Add(SqlHelper.CreateParameterObject("@parentID", SqlDbType.NVarChar, ParameterDirection.Input, IDDonVi + ";"));
                    lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                    MyReportSQL = "SELECT A.TenDM,SUM(M1.F1),SUM(M1.F2),SUM(M1.F3),SUM(M1.F4),SUM(M1.F5),SUM(M1.F6),SUM(M1.F7),SUM(M1.F8),SUM(M1.F9),SUM(M1.F10),SUM(M1.F11),SUM(M1.F12),SUM(M1.F13),SUM(M1.F14),SUM(M1.F15),SUM(M1.F16),SUM(M1.F17),SUM(M1.F18),SUM(M1.F19),SUM(M1.F20),SUM(M1.F21),SUM(M1.F22),SUM(M1.F23)," +
                                  "SUM(M1.F24),SUM(M1.F25),SUM(M1.F26),SUM(M1.F27),SUM(M1.F28),SUM(M1.F29),SUM(M1.F30),SUM(M1.F31),SUM(M1.F32),SUM(M1.F33),A.BoiDen,A.LaToChuc,A.FatherID,A.STT " +
                                  "FROM dbo.tblBaoCaoMau8 M1 INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                                  "WHERE KyBaoCaoID_DM = @KyBaoCao AND DonViID IN (Select ID From tblDonVi Where parentID Like @parentID+'%' Or ParentID Like +'%;'+@parentID+'%') " +
                                  "GROUP BY A.TenDM,A.BoiDen,A.LaToChuc,A.STT,A.FatherID,A.ID " +
                                  "ORDER BY A.ID";
                }
                else
                {
                    lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                    lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                    MyReportSQL = "SELECT M1.ID,A.TenDM,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F22,F23,"+
                                  "F24,F25,F26,F27,F28,F29,F30,F31,F32,F33,A.BoiDen,A.LaToChuc,A.FatherID,A.STT " +
                                  "FROM dbo.tblBaoCaoMau8 M1 INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                                  "WHERE KyBaoCaoID_DM = @KyBaoCao AND DonViID = @DonViID";

                }
                MyReportParams = lstParams;
                MyReportSQLParam = "SELECT BC.CM_TenBaoCao AS TenBaoCao,BC.CM_SoBaoCao AS SoBaoCao ,BC.CreateDate AS TuNgay,BC.UpdateDate AS DenNgay,BC.CM_NguoiLapBaoCao AS NguoiLapBaoCao,BC.CM_ThuTruong AS BKTDU,BC.CM_NgayBaoCao AS NgayBaoCao," +
                                   "DV.TenDV FROM dbo.tblBaoCaoMau8 BC ,tblDonVi DV " +
                                   "WHERE BC.DonViID = DV.ID AND BC.KyBaoCaoID_DM = @KyBaoCao AND BC.DonViID = @DonViID AND BC.IsCommon = 1";
                string UnionParam = " UNION ALL SELECT '','',GETDATE(),GETDATE(),'','',GETDATE(),N'" + trvDonVi.SelectedNode.Text + "' WHERE NOT EXISTS (" + MyReportSQLParam + ")";
                MyReportSQLParam += UnionParam;
                MyReportName = "rpt08.rpt";
                MyTableName = "tblBaoCaoMau7";
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "window.open('MyReportView.aspx');", true);
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("index.aspx");
        }
    }
}
